package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private SaleListGoodsDao saleListGoodsDao;


    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    /**
     * 分页查询商品库存信息
     * @param page
     * @param rows
     * @param codeOrName
     * @param goodsTypeId
     * @return
     */
    @Override
    public Map<String, Object> getListInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        Map<String, Object> goodsMap = new HashMap<>();
        PageHelper.startPage(page,rows);

        //分页查询
        Page<Goods> goodsPage = goodsDao.selectGoodsInfo(codeOrName, goodsTypeId);

        List<Goods> goodsList = goodsPage.getResult();

        goodsList.stream().forEach(goods -> {
            //
            Integer saleTotal = saleListGoodsDao.getSaleTotalByGoodsId(goods.getGoodsId());
            goods.setSaleTotal(saleTotal);
        });


        goodsMap.put("total",goodsPage.getTotal());
        goodsMap.put("rows",goodsPage.getResult());
        return goodsMap;
    }

    /**
     * 分页查询商品信息
     *
     * @param page
     * @param rows
     * @param goodsName
     * @param goodsTypeId
     * @return
     */
    @Override
    public Map<String, Object> getGoodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        HashMap<String, Object> map = new HashMap<>();
        // 开启分页
        PageHelper.startPage(page, rows);
        Page<Goods> goodsPage = goodsDao.selectGoods(goodsName, goodsTypeId);
        map.put("total", goodsPage.getTotal());
        map.put("rows", goodsPage.getResult());
        return map;
    }

    /**
     * 添加或修改商品信息
     *
     * @param goods
     */
    @Override
    public void saveGoods(Goods goods) {
        Integer goodsId = goods.getGoodsId();
        if (goodsId == null) {
            goods.setInventoryQuantity(0);
            goods.setState(0);
            goodsDao.insertGoods(goods);
        } else {
            goodsDao.updateGoods(goods);
        }
    }

    /**
     * 删除商品信息（要判断商品状态,入库、有进货和销售单据的不能删除）
     *
     * @param goodsId
     */
    @Override
    public boolean deleteGoods(Integer goodsId) {
        //先查询
        Goods goods = goodsDao.selectGoodsById(goodsId);
        if (goods.getState() == 0) {
            //可以删除
            goodsDao.deleteGoodsById(goodsId);
            return true;
        } else {
            System.out.println("删除失败，有进货和销售单据不能删除");
            return false;
        }

    }


    /**
     * 获取无库存商品信息
     *
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, rows);
        Page<Goods> goodsPage = goodsDao.selectNoInventoryGoods(nameOrCode);
        map.put("total", goodsPage.getTotal());
        map.put("rows", goodsPage.getResult());
        return map;
    }

    /**
     * 获取有库存商品信息
     *
     * @param page
     * @param rows
     * @param nameOrCode
     * @return
     */
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, rows);
        Page<Goods> goodsPage = goodsDao.selectHasInventoryGoods(nameOrCode);
        map.put("total", goodsPage.getTotal());
        map.put("rows", goodsPage.getResult());
        return map;

    }

    /**
     * 添加商品期初库存
     *
     * @param goodsId
     * @param inventoryQuantity
     * @param purchasingPrice
     */
    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.insertStock(goodsId, inventoryQuantity, purchasingPrice);
    }

    /**
     * 删除库存
     *
     * @param goodsId
     */
    @Override
    public boolean deleteStock(Integer goodsId) {
        Goods goods = goodsDao.selectGoodsById(goodsId);
        if (goods.getState() == 0) {
            goodsDao.deleteStockById(goodsId);
            return true;
        } else {
            return false;
        }


    }


}
